 //控制层 
app.controller('consociationController' ,function($scope,$controller,$location,consociationService){	
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		consociationService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		consociationService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		consociationService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.csId!=null){//如果有ID
			serviceObject=consociationService.update( $scope.entity ); //修改  
		}else{
			serviceObject=consociationService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	alert(response.message);
					window.location = "companyInfo-brand.html";
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	//选中全部
	$scope.selectAll = function($event) {
		if($event.target.checked) { //如果是被选中,则增加到数组
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				$scope.selectIds.push(each.csId); //将空数组中push新的数据，符合条件的数据的id值
			});
		} else {
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				var idx = $scope.selectIds.indexOf(each.csId);
				$scope.selectIds.splice(idx, 1); //删除 
			});
		}

	}
	 
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		consociationService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
					$scope.selectIds=[];
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		consociationService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
    
    
    //编辑页面携带参数
	$scope.edit = function(){
		var id = $location.absUrl().split("?id=")[1];
		$scope.findOne(id);
	}
	  
    //单个删除
	$scope.delsingle = function(csId){
		consociationService.dele(csId).success(
			function(response){
				if(response.success){
					//重新查询 
					alert(response.message)
		        	window.location = "companyInfo-brand.html";
				}else{
					alert(response.message);
				}			
			}
		);
	}
	
	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//提交课程前先保存图片到数据库,并拿到相对路径
		if($scope.entity.image != null){
			consociationService.upload().then(function (response) {
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.csLogo = response.data.data.src;
				$scope.save();
				}
			);
		}else{
			$scope.save();
		}
	}
    
    //编辑页面携带参数
	$scope.edit = function(){
		var id = $location.absUrl().split("?id=")[1];
		$scope.findOne(id);
	}
});	
